<?php

declare(strict_types=1);

namespace App\Service;

use App\Constants\ErrorCode;
use App\Library\Common;
use App\Model\FlyShopFans;
use Hyperf\Di\Annotation\Inject;
use App\Model\FlyShop;
use function Swoole\Coroutine\Http\request;

/**
 * 商家粉丝
 * Class FlyShopFansService
 * @package App\Service
 */
class FlyShopFansService extends BaseService
{


//    商铺获取粉丝数
    public function getShopFans(int $shop_id,$type=1)
    {
        $fans_num = 0;
        $Instance = $this->getModelInstance();
//            获取取用户进行过滤
        $cancel_ids  =  $Instance
            ->where('shop_id',$shop_id)
            ->where('type',1)
            ->where('is_cancel',1)
            ->groupBy(['user_id'])->pluck('user_id');
        $cancel_ids = $cancel_ids?$cancel_ids->toArray():[];

//        获取关注数去重
        $fans_num  = $Instance
            ->whereNotIn('user_id',$cancel_ids)
            ->where('shop_id',$shop_id)
            ->where('is_cancel',0)
            ->groupBy(['user_id']);
        if($type==1){
            $fans_num = $fans_num?$fans_num
                ->count('user_id'):0;
            return $fans_num;
        }else{
            return $fans_num->with(['user_info','shop_info']);
        }
    }

//    关注操作

    /**
     * @param array $params
     * @return int
     */
    public function addFans(array $params)
    {
//            处理关注
//            关注的，查询是否已经关注，或已取关
            $params['is_cancel'] = 0;
            if($params['resource_type']==2){
                 FlyShopFans::query()->updateOrInsert([
                    'user_id'=>$params['user_id'],
                    'type'=>$params['type'],
                    'resource_type'=>$params['resource_type'],
                    'shop_id'=>$params['shop_id'],
                ],Common::filter_null($params));
            }else{
                 FlyShopFans::query()->updateOrInsert([
                    'user_id'=>$params['user_id'],
                    'type'=>$params['type'],
                    'resource_type'=>$params['resource_type'],
                    'resource_id'=>$params['resource_id'],
                ],Common::filter_null($params));
            }
        return true;
    }

//    取消关注
    public function cancelFans(int $user_id,int $link_id ,$type,$resource_type=1)
    {
        if($resource_type==2){
            $fans = FlyShopFans::query()
                ->where('user_id',$user_id)
                ->where('shop_id',$link_id)
                ->where('type',$type)
                ->where('resource_type',$resource_type)
                ->first();
            if($fans){
                $fans->is_cancel = 1;
                $fans->cancel_at = date('Y-m-d H:i:s');
                return  $fans->save();
            }else{
                return false;
            }
        }else{
            $fans = FlyShopFans::query()
                ->where('user_id',$user_id)
                ->where('resource_id',$link_id)
                ->where('type',$type)
                ->where('resource_type',$resource_type)
                ->first();
            if($fans){
                $fans->is_cancel = 1;
                $fans->cancel_at = date('Y-m-d H:i:s');
                return  $fans->save();
            }else{
                return false;
            }
        }


    }

//    点赞收藏等
    public function addPoint(array $user_id,$shop_id, $type=1)
    {

    }

    //    点赞收藏等
    public function cancelPoint(array $user_id,$resource_id,$type=2)
    {
        $point = FlyShopFans::query()
            ->where('user_id',$user_id)
            ->where('resource_id',$resource_id)
            ->where('type',$type)
            ->first();
        if($point){
            $point->is_cancel = 1;
            $point->cancel_at = date('Y-m-d H:i:s');
            return  $point->save();
        }else{
            return false;
        }
    }


    //    获取用户关注的商铺列表
    public function getStar(int $user_id,$key_word='',$type=1,$resource_type=2)
    {
        $where = [
            'user_id'=>$user_id,
            'type'=>$type,
            'is_cancel'=>0,
            'resource_type'=>$resource_type,
        ];

        $shop_ids = [];
        if($key_word){
          $shop_ids =   FlyShop::query()->where('shop_name','like','%'.$key_word.'%')->pluck('id');
          $shop_ids = $shop_ids?$shop_ids->toArray():[];
        }
        return  FlyShopFans::query()
            ->where($where)
            ->when($key_word!='',function ($query) use($shop_ids) {
                $query->whereIn('shop_id',$shop_ids);
            })
            ->with('shop_info')->select();
    }
    //    获取用户关注的数量
    public function getStarNum(int $user_id)
    {
        $where = [
            'user_id'=>$user_id,
            'type'=>1,
            'is_cancel'=>0,
            'resource_type'=>2,
        ];
        return  FlyShopFans::query()
            ->where($where)
            ->select()->count();
    }

}